import Vue from 'vue'
import { getCookie } from '../assets/js/cookieOpt'
import Router from 'vue-router'
// import Home from '../view/home.vue'
// import Login from '../view/login.vue'
// import Edit from '../view/edit.vue'

Vue.use(Router);

const router = new Router({
    mode: 'hash',
    routes: [
        {
            name: 'home',
            path: '/home',
            component: () => import('../view/home.vue'),
        },
        {
            name: 'login',
            path: '/login',
            component: () => import('../view/login.vue'),
        },
        {
            name: 'detail',
            path: '/detail',
            component: () => import('../view/detail.vue'),
        },
        {
            name: 'edit',
            path: '/edit',
            component: () => import('../view/edit.vue'),
        },
        {
            path: '/add',
            component: () => import('../view/edit.vue'),
        },
        {
            path: '/',
            component: () => import('../App.vue'),
            redirect: '/home'
        },
    ]
});

// 全局前置守卫
router.beforeEach((to, from, next) =>  {
    let token = getCookie('token');
    
    if (to.name !== 'login' && !token) next({ name: 'login' });
    else next()
});

export default router;
